<?php
# 文件名称:behavior.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_HTMLBehavior
{
	/**
	 * 加载mootools框架
	 */
	function mootools($debug = null)
	{
		static $loaded;
		// Only load once
		if ($loaded) {
			return;
		}
		$konkcheck = isset($_SERVER['HTTP_USER_AGENT']) ? strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "konqueror") : null;
		if ($debug || $konkcheck) {
			C_HTML::script('mootools-uncompressed.js', 'media/system/js/', false);
		} else {
			C_HTML::script('mootools.js', 'media/system/js/', false);
		}
		$loaded = true;
		return;
	}
		function caption() {
		C_HTML::script('caption.js');
	}
	function formvalidation() {
		C_HTML::script('validate.js' );
	}
	function switcher() {
		C_HTML::script('switcher.js' );
	}
	function combobox() {
		C_HTML::script('combobox.js' );
	}

	function uploader($id='file-upload', $params = array())
	{
		C_HTML::script('swf.js' );
		C_HTML::script('uploader.js' );
		static $uploaders;
		if (!isset($uploaders)) {
			$uploaders = array();
		}
		if (isset($uploaders[$id]) && ($uploaders[$id])) {
			return;
		}
		// Setup options object
		$opt['url']					= (isset($params['targetURL'])) ? $params['targetURL'] : null ;
		$opt['swf']					= (isset($params['swf'])) ? $params['swf'] : JURI::root(true).'/media/system/swf/uploader.swf';
		$opt['multiple']			= (isset($params['multiple']) && !($params['multiple'])) ? '\\false' : '\\true';
		$opt['queued']				= (isset($params['queued']) && !($params['queued'])) ? '\\false' : '\\true';
		$opt['queueList']			= (isset($params['queueList'])) ? $params['queueList'] : 'upload-queue';
		$opt['instantStart']		= (isset($params['instantStart']) && ($params['instantStart'])) ? '\\true' : '\\false';
		$opt['allowDuplicates']		= (isset($params['allowDuplicates']) && !($params['allowDuplicates'])) ? '\\false' : '\\true';
		$opt['limitSize']			= (isset($params['limitSize']) && ($params['limitSize'])) ? (int)$params['limitSize'] : null;
		$opt['limitFiles']			= (isset($params['limitFiles']) && ($params['limitFiles'])) ? (int)$params['limitFiles'] : null;
		$opt['optionFxDuration']	= (isset($params['optionFxDuration'])) ? (int)$params['optionFxDuration'] : null;
		$opt['container']			= (isset($params['container'])) ? '\\$('.$params['container'].')' : '\\$(\''.$id.'\').getParent()';
		$opt['types']				= (isset($params['types'])) ?'\\'.$params['types'] : '\\{\'All Files (*.*)\': \'*.*\'}';

		// Optional functions
		$opt['createReplacement']	= (isset($params['createReplacement'])) ? '\\'.$params['createReplacement'] : null;
		$opt['onComplete']			= (isset($params['onComplete'])) ? '\\'.$params['onComplete'] : null;
		$opt['onAllComplete']		= (isset($params['onAllComplete'])) ? '\\'.$params['onAllComplete'] : null;
/*  types: Object with (description: extension) pairs, default: Images (*.jpg; *.jpeg; *.gif; *.png)
 */
		$options = C_HTMLBehavior::_getJSObject($opt);
		// Attach tooltips to document
		$document =& JFactory::getDocument();
		$uploaderInit = 'sBrowseCaption=\''.'Browse Files'.'\';
				sRemoveToolTip=\''.'Remove from queue'.'\';
				window.addEvent(\'load\', function(){
				var Uploader = new FancyUpload($(\''.$id.'\'), '.$options.');
				$(\'upload-clear\').adopt(new Element(\'input\', { type: \'button\', events: { click: Uploader.clearList.bind(Uploader, [false])}, value: \''.'Clear Completed'.'\' }));				});';
		$document->addScriptDeclaration($uploaderInit);
		// Set static array
		$uploaders[$id] = true;
		return;
	}
	function calendar()
	{
		C_HTML::stylesheet('calendar.css', 'media/system/css/');
		C_HTML::script( 'calendar.js', 'media/system/js/' );
		$translation = C_HTMLBehavior::_calendartranslation();
		if($translation) {
			$SYS =& get_instance();
			$SYS->doc->addScriptDeclaration($translation);
		}
	}
	/**
	 * Keep session alive, for example, while editing or creating an article.
	 */
	function keepalive()
	{
		// Include mootools framework
		C_HTMLBehavior::mootools();
		$config 	 =& JFactory::getConfig();
		$lifetime 	 = ( $config->getValue('lifetime') * 60000 );
		$refreshTime =  ( $lifetime <= 60000 ) ? 30000 : $lifetime - 60000;
		//refresh time is 1 minute less than the liftime assined in the configuration.php file
		$document =& JFactory::getDocument();
		$script  = '';
		$script .= 'function keepAlive( ) {';
		$script .=  '	var myAjax = new Ajax( "index.php", { method: "get" } ).request();';
		$script .=  '}';
		$script .= 	' window.addEvent("domready", function()';
		$script .= 	'{ keepAlive.periodical('.$refreshTime.' ); }';
		$script .=  ');';
		$document->addScriptDeclaration($script);
		return;
	}
	/**
	 * Internal method to get a JavaScript object notation string from an array
	 *
	 */
	function _getJSObject($array=array())
	{
		// Initialize variables
		$object = '{';
		// Iterate over array to build objects
		foreach ((array)$array as $k => $v)
		{
			if (is_null($v)) {
				continue;
			}
			if (!is_array($v) && !is_object($v)) {
				$object .= ' '.$k.': ';
				$object .= (is_numeric($v) || strpos($v, '\\') === 0) ? (is_numeric($v)) ? $v : substr($v, 1) : "'".$v."'";
				$object .= ',';
			} else {
				$object .= ' '.$k.': '.C_HTMLBehavior::_getJSObject($v).',';
			}
		}
		if (substr($object, -1) == ',') {
			$object = substr($object, 0, -1);
		}
		$object .= '}';
		return $object;
	}
	/**
	 * Internal method to translate the JavaScript Calendar
	 *
	 */
	function _calendartranslation()
	{
		static $jsscript = 0;
		if ($jsscript == 0)
		{
			$return = 'Calendar.LANG("", "", {
        fdow: 1,                // first day of week for this locale; 0 = Sunday, 1 = Monday, etc.
        goToday: "'.C_text::_('GO TODAY').'",
        today: "'.C_text::_('TODAY').'",         // appears in bottom bar
        wk: "wk",
        weekend: "0,6",         // 0 = Sunday, 1 = Monday, etc.
        AM: "am",
        PM: "pm",
        mn : [ "'.C_text::_('JANUARY').'",
               "'.C_text::_('FEBRUARY').'",
               "'.C_text::_('MARCH').'",
               "'.C_text::_('APRIL').'",
               "'.C_text::_('MAY').'",
               "'.C_text::_('JUNE').'",
               "'.C_text::_('JULY').'",
               "'.C_text::_('AUGUST').'",
               "'.C_text::_('SEPTEMBER').'",
               "'.C_text::_('OCTOBER').'",
               "'.C_text::_('NOVEMBER').'",
               "'.C_text::_('DECEMBER').'" ],
        smn : [ "'.C_text::_('JANUARY_SHORT').'",
                "'.C_text::_('FEBRUARY_SHORT').'",
                "'.C_text::_('MARCH_SHORT').'",
                "'.C_text::_('APRIL_SHORT').'",
                "'.C_text::_('MAY_SHORT').'",
                "'.C_text::_('JUNE_SHORT').'",
                "'.C_text::_('JULY_SHORT').'",
                "'.C_text::_('AUGUST_SHORT').'",
                "'.C_text::_('SEPTEMBER_SHORT').'",
                "'.C_text::_('OCTOBER_SHORT').'",
                "'.C_text::_('NOVEMBER_SHORT').'",
                "'.C_text::_('DECEMBER_SHORT').'" ],
        dn : [ "'.C_text::_('SUNDAY').'",
               "'.C_text::_('MONDAY').'",
               "'.C_text::_('TUESDAY').'",
               "'.C_text::_('WEDNESDAY').'",
               "'.C_text::_('THURSDAY').'",
               "'.C_text::_('FRIDAY').'",
               "'.C_text::_('SATURDAY').'",
               "'.C_text::_('SUNDAY').'" ],
        sdn : [ "'.C_text::_('SUN').'",
                "'.C_text::_('MON').'",
                "'.C_text::_('TUE').'",
                "'.C_text::_('WED').'",
                "'.C_text::_('THU').'",
                "'.C_text::_('FRI').'",
                "'.C_text::_('SAT').'",
                "'.C_text::_('SUN').'" ]
});';
			$jsscript = 1;
			return $return;
		} else {
			return false;
		}
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>
